<?php
namespace App\Common\Lib\Com\Timer\TimeWheelPools;

use App\Common as com;
use App\Common\Lib\Com\Timer\TimeWheelPools\Item as thisItem;

class Item extends com\Lib\LibBase {

	private static int $varItemDataIndex_TaskId = 0;

	private static int $varItemDataIndex_TaskFn = 1;

	private static int $varItemDataIndex_TimeMs = 2;

	private com\Lib\Fun\Array_\SortArray $poolSortArray;

	private array $pool = [];

	public function __construct(  ) {
		$this->poolSortArray = com\Lib\Fun\Array_\SortArray::make( [], 2, 'asc' );
		//
	    parent::__construct();
	}

	/**
	 * 推入
	 * @param int $taskId
	 * @param \Closure $taskFn
	 * @param int $timeMs
	 * @return $this
	 */
	public function push( int $taskId, \Closure $taskFn, int $timeMs ):self {
		$this->poolSortArray->push( [
			self::$varItemDataIndex_TaskId => $taskId,
			self::$varItemDataIndex_TaskFn => $taskFn,
			self::$varItemDataIndex_TimeMs => $timeMs,
		] );
		//
		return $this;
	}

	/**
	 * 获取并删除最近时间的元素
	 * @return mixed
	 */
	public function popAsRecently(  ) {
		return $this->poolSortArray->popAsHead();
	}

	/**
	 * 获取最近时间的元素
	 * @return mixed
	 */
	public function getAsRecently(  ) {
		return $this->poolSortArray->getAsHead();
	}

	/**
	 * 删除
	 * @param int $taskId
	 * @return bool 是否删除成功。[false]数据不存在
	 */
	public function rm( int $taskId ):bool {
		foreach( $this->poolSortArray->getArrayQuote() as $taskDataKey => $taskData ){
			if( $taskData[ self::$varItemDataIndex_TaskId ] === $taskId ){
				unset( $this->poolSortArray->getArrayQuote()[ $taskDataKey ] );
				//
				return true;
			}
		}
		//
		return false;
	}

}
